package com.yuwei.home

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.yuwei.common.entity.SimpleVideoEntity
import com.yuwei.common.entity.SimpleVideoEntityItem
import com.yuwei.common.mvi.BaseViewModel
import com.yuwei.home.model.HomePagingSource
import kotlinx.coroutines.flow.Flow

class HomeViewModel: BaseViewModel<HomeApiService, HomeIntent>() {
    override fun handleIntent(intent: HomeIntent) {
        when(intent){
            is HomeIntent.getSimpleType -> {
                httpRequest(apiService.getSimpleType(),"getSimpleType")
            }
            is HomeIntent.getSimpleVideoByChannelId->{
                httpRequest(apiService.getSimpleVideoByChannelId(intent.channelId,intent.page,intent.pageSize),"getSimpleVideoByChannelId")
            }
            is HomeIntent.getComment ->{
                httpRequest(apiService.getComment(intent.datatype,intent.itemid),"getCommentList")
            }
        }
    }
    fun loadSimpleVideo(typeId: String): Flow<PagingData<SimpleVideoEntityItem>> {
        return Pager(
            config = PagingConfig(
                initialLoadSize = 1,
                pageSize = 5,
                enablePlaceholders = false
            ),
            pagingSourceFactory = {
                HomePagingSource(typeId)
            }
        ).flow
    }
}